package br.com.app;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public final class DataBase {

	private static final String path = System.getProperty("user.home") + "\\AppData\\Local\\Nielle";

	public static List<Coisa> load() {
		List<Coisa> coisas = new ArrayList<Coisa>();
		try {
			File f = new File(path + "\\Base.nl");
			Scanner in = new Scanner(f);

			String[] s;
			Coisa c;

			while (in.hasNextLine()) {
				s = in.nextLine().split(".::.");
				c = new Coisa();
				c.setId(s[0]);
				c.setQuando(s[1]);
				c.setOnde(s[2]);
				c.setoQue(s[3]);
				c.setComQuem(s[4]);
				coisas.add(c);
			}

			in.close();

		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}

		return coisas;
	}

	public static void save(List<Coisa> coisas) {
		File diretorio = new File(path);
		if (!diretorio.exists()) {
			diretorio.mkdirs();
		}
		try {
			FileWriter f = new FileWriter(path + "\\Base.nl");
			BufferedWriter out = new BufferedWriter(f);
			for (Coisa c : coisas) {
				out.write(c.toString());
				out.newLine();
			}
			out.close();
			f.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

}